﻿Imports System.Data.OleDb

Public Class ProfileManager
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            If (Not Page.IsPostBack) Then
                bindDates()
                fillFields()







            End If



        Catch ex As Exception

        End Try
    End Sub

    Private Sub fillFields()
        Try

        

        Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
        Dim profileManager As ProfilesManagement = New ProfilesManagement()
        Dim dr As OleDbDataReader = profileManager.SelectProfileByUserID(userId)
        Dim dob As DateTime
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("ProfileName")) Then txtName.Text = dr("ProfileName")
            If Not IsDBNull(dr("ProfileSurname")) Then txtSurname.Text = dr("ProfileSurname")
            If Not IsDBNull(dr("ProfileHobbies")) Then txtHobbies.Text = dr("ProfileHobbies")
            If Not IsDBNull(dr("ProfileDob")) Then dob = dr("ProfileDob")

            DropDownDay.SelectedValue = dob.Day
            DropDownMonth.SelectedValue = dob.Month
            DropDownYear.SelectedValue = dob.Year

        End If
        Catch ex As Exception

        End Try


    End Sub

    Private Sub bindDates()
        Try

            Dim dateBind As New ArrayList()
            Dim month As New ArrayList()
            Dim year As New ArrayList()

            For i As Integer = 1 To 31
                dateBind.Add(Convert.ToString(i))
            Next

            For i As Integer = 1 To 12
                month.Add(Convert.ToString(i))
            Next

            For i As Integer = 1940 To DateTime.Now.Year
                year.Add(Convert.ToString(i))
            Next

            DropDownDay.DataSource = dateBind
            DropDownMonth.DataSource = month
            DropDownYear.DataSource = year

            DropDownDay.DataBind()
            DropDownMonth.DataBind()
            DropDownYear.DataBind()

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Try

       
        Dim bornDate As New DateTime(DropDownYear.SelectedValue, DropDownMonth.SelectedValue, DropDownDay.SelectedValue)

        Dim myGuid As New Guid
        myGuid = Guid.NewGuid()

        Dim fileIsAnImage As Boolean = False
        Dim fileName As String
        Dim myFileName As String = String.Empty

        If (fileUpl.HasFile) Then

            fileName = Server.HtmlEncode(fileUpl.FileName)
            Dim extension As String = System.IO.Path.GetExtension(fileName)

            If (extension = ".jpg") Then
                myFileName = myGuid.ToString() + ".jpg"
                fileIsAnImage = True
            ElseIf (extension = ".png") Then
                myFileName = myGuid.ToString() + ".png"
                fileIsAnImage = True
            End If
        

            If (fileIsAnImage) Then
                Dim filePath = Request.MapPath("Pictures/Avatars") + "/" + myFileName
                fileUpl.SaveAs(filePath)
                'edit the database
                'get userID by username
                Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
                Dim profileUpdate As ProfilesManagement = New ProfilesManagement()
                profileUpdate.EditProfileByUserID(userId, txtName.Text, txtSurname.Text, bornDate, "Pictures/Avatars/" + myFileName, txtHobbies.Text)

                lblMessage.Text = "Update done"

            Else
                lblMessage.Text = "Only jpeg or png files are allowed"
            End If
        Else

            Dim profileUpdate As ProfilesManagement = New ProfilesManagement()
            Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
            profileUpdate.EditProfileByUserIDWithoutImage(userId, txtName.Text, txtSurname.Text, bornDate, txtHobbies.Text)
            lblMessage.Text = "Update is done"

        End If
        Catch ex As Exception
            lblMessage.Text = ex.Message
        End Try

    End Sub
End Class